package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.FenXiangLieBiaoModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyli on 2017/11/25.
 */

public class ShareListAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<FenXiangLieBiaoModel.CoursesharesBean> dataList = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private MyClickListener mListener;

    @Override
    public void onClick(View v) {
        mListener.clickListener(v);
    }

    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }

    public ShareListAdapter(Context context, List<FenXiangLieBiaoModel.CoursesharesBean> dataList, MyClickListener listener) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);
        this.mListener = listener;
        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<FenXiangLieBiaoModel.CoursesharesBean> data) {
        if (data != null) {
//            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        FenXiangLieBiaoModel.CoursesharesBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.sharelist_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.lesson_type.setText(bean.getLabel());
        viewHolder.lesson_title.setText(bean.getTitle());
        viewHolder.lesson_price.setText("价格：" + bean.getPrice());
        viewHolder.lesson_fan.setText(bean.getFandian());
        viewHolder.lesson_money.setText("¥" + bean.getYongjin());
        imageLoader.displayImage(bean.getLargePicture(), viewHolder.lessonImg, options);


        viewHolder.lesson_type.setTag(i);
        viewHolder.lesson_type.setOnClickListener(this);
        viewHolder.lesson_title.setTag(i);
        viewHolder.lesson_title.setOnClickListener(this);
        viewHolder.lesson_price.setTag(i);
        viewHolder.lesson_price.setOnClickListener(this);
        viewHolder.lesson_fan.setTag(i);
        viewHolder.lesson_fan.setOnClickListener(this);
        viewHolder.lesson_money.setTag(i);
        viewHolder.lesson_money.setOnClickListener(this);


        viewHolder.lessonImg.setTag(i);
        viewHolder.lessonImg.setOnClickListener(this);

        viewHolder.fenxinag.setTag(i);
        viewHolder.fenxinag.setOnClickListener(this);

        return view;
    }


    class ViewHolder {
        ImageView lessonImg;
        TextView lesson_type;
        TextView lesson_title;
        TextView lesson_price;
        TextView lesson_fan;
        TextView lesson_yongjin;
        TextView lesson_money;
        ImageView fenxinag;

        public ViewHolder(View view) {
            lessonImg = view.findViewById(R.id.lesson_img);
            lesson_type = view.findViewById(R.id.lesson_type);
            lesson_title = view.findViewById(R.id.lesson_title);
            lesson_price = view.findViewById(R.id.lesson_price);
            lesson_fan = view.findViewById(R.id.lesson_fan);
            lesson_yongjin = view.findViewById(R.id.lesson_yongjin);
            lesson_money = view.findViewById(R.id.lesson_money);
            fenxinag = view.findViewById(R.id.fenxinag);
        }
    }

}
